package top.snowlands.jobs;

import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;
import top.snowlands.constant.RedisConstant;
import top.snowlands.utils.QiniuUtils;

import java.util.Set;

/**
 * 自定义Job 实现定时清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        //根据集合插值计算，获得垃圾图片名称集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_DB_RESOURCES, RedisConstant.SETMEAL_PIC_RESOURCES);
        System.out.println("删除图片");
        if(set!=null){
            for(String picName:set){
                //删除七牛云服务器
                QiniuUtils.deleteFileFromQiniu(picName);
                //从Redis集合中删掉图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            }
        }
    }
}
